package com.catmiw.swt;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Properties;
import java.util.Random;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Shell;

import com.catmiw.wyz.util.DateTool;
import com.catmiw.wyz.util.ExcelUtils;

public class DialogBasicCompute extends BasicDialog {
	public DialogBasicCompute(Shell parent) {
		super(parent);
		rowposition = new int[] { 65, 35 };
		columnposition = new int[] { 100 };
	}

	static String[] kind = { "100以内加减法", "1位数乘法", "100以内1位数乘2位数", "100以内1位除数除法" };

	private List expressions[] = { new ArrayList<String>(),
			new ArrayList<String>(), new ArrayList<String>(),
			new ArrayList<String>() };

	private Button[] btKind;
	private Button btConfirm, btQuit;

	private List<String> createExpressions(){
		boolean choose=false;
		
		if (btKind[0].getSelection()){
			choose=true;
			if (expressions[0].size()==0){
			for (int i=0;i<=100;i++){
				for (int j=i;j<=100-i;j++){
					expressions[0].add(i+" + "+j);
				}
				for (int j=0;j<=i;j++){
					expressions[0].add(i+" - "+j);
				}
			}
			}
		}
		if (btKind[1].getSelection()){
			choose=true;
			if (expressions[1].size()==0){
			for (int i=1;i<10;i++){
				for (int j=1;j<10;j++){
					expressions[1].add(i+" × "+j);
				}
			}
			}
		}
		if (btKind[2].getSelection()){
			choose=true;
			if (expressions[2].size()==0){
			for (int i=10;i<=100;i++){
				for (int j=2;j<10;j++){
					if (i*j<=100){
						expressions[2].add(i+" × "+j);
					} else {
						break;
					}
				}
			}
			}
		}
		if (btKind[3].getSelection()){
			choose=true;
			if (expressions[3].size()==0){
			for (int i=1;i<=100;i++){
				for (int j=2;j<i;j++){
					if (i%j==0&&j<10)
						expressions[3].add(i+" ÷ "+j);
				}
			}
			}
		}
		
		if (!choose)
			return null;
		
		List<String> temp=new ArrayList<String>();
		
		Random rand=new Random();

		while(temp.size()<100){
			for (int i=0;i<btKind.length;i++){
				if (btKind[i].getSelection()){
					temp.add(expressions[i].get(rand.nextInt(expressions[i].size())).toString());
				}
				if (temp.size()>=100)
					break;
			}
		}
		
		Collections.sort(temp,new Comparator<String>(){
			public int compare(String o1, String o2) {
				Random random=new Random();
				return random.nextInt(1000)-random.nextInt(1000);
			}
		});
		return temp;
	}

	@Override
	public void createContents() {
		shell.setText("基础算术训练");
		shell.setLayout(new FormLayout());

		btKind = new Button[kind.length];
		Group group = createGroupCheckChooser(shell, btKind, "选择算术题类型", kind,
				SWT.CHECK, new int[] { 0 });
		group.setLayout(new GridLayout(2, true));
		setPosition(group, 1, 1);

		Composite commandbuttons = createCommandButtons(shell);
		setPosition(commandbuttons, 2, 1);

		shell.setSize(350, 170);
		shell.setLocation(
				Display.getCurrent().getClientArea().width / 2
						- shell.getSize().x / 2,
				Display.getCurrent().getClientArea().height / 2
						- shell.getSize().y / 2);
	}

	public Composite createCommandButtons(Composite parent) {
		Composite composite = new Composite(parent, SWT.NONE);
		composite.setLayout(new FormLayout());

		btConfirm = new Button(composite, SWT.NONE);
		btConfirm.setText("生成");

		btQuit = new Button(composite, SWT.NONE);
		btQuit.setText("退出");

		FormData data = new FormData();
		data.right = new FormAttachment(100, 0 - COMPOSITESPACE);
		btQuit.setLayoutData(data);

		data = new FormData();
		data.right = new FormAttachment(btQuit, 0 - COMPOSITESPACE);
		btConfirm.setLayoutData(data);

		return composite;
	}

	private String createTrainbook(String begin,String end){
		for (Date d=DateTool.toDate(begin);d.compareTo(DateTool.toDate(end))<=0;d=DateTool.dateAfterDays(d, 1)){
			createTrainbook(DateTool.dateToString(d, "yyyy-MM-dd"));
		}
		return null;
	}
	
	private String createTrainbook(String date) {
		List<String> test = createExpressions();
		if (test == null)
			return "未选中任何训练";

		String filename = properties.getProperty("模版.目录")
				+ properties.getProperty("模版.算术训练");

		Workbook wb = ExcelUtils.getWorkbook(filename);
		Sheet sheet = wb.getSheetAt(0);
		Row row = sheet.getRow(1);
		Cell cell = row.getCell(0);
		//String today = new SimpleDateFormat("yyyy-MM-dd").format(new Date());

		cell.setCellValue(date);

		for (int i = 0; i < 25; i++) {
			row = sheet.getRow(i + 2);
			for (int j = 0; j < 4; j++) {
				cell = row.getCell(j * 4 + 1);
				cell.setCellValue(test.get(i * 4 + j));
			}
		}
		String outfilename = properties.getProperty("输出目录.算术训练") + "基础算术训练"
				+ date + ".xls";
		ExcelUtils.writeWorkbook(wb, outfilename);
		return null;
	}

	@Override
	public void addListeners() {
		btQuit.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				shell.dispose();
			}
		});

		btConfirm.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				//String rtn = createTrainbook(DateTool.dateToString(new Date(), "yyyy-MM-dd"));
				String rtn = createTrainbook("20130127","20130224");
				if (rtn != null) {
					messagebox("错误", rtn);
				} else {
					messagebox("提示", "生成训练题完成！");
				}
			}
		});

	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Display display = new Display();
		final Shell shell = new Shell(display);

		DialogBasicCompute dbc = new DialogBasicCompute(shell);
		Properties p = new Properties();
		try {
			p.load(new FileReader(
					"d:/studyassist/config/studyassist.properties"));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		dbc.setProperties(p);
		dbc.open();
	}

}
